package com.zhou.jiancai.fragment;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.yarolegovich.discretescrollview.DiscreteScrollView;
import com.yarolegovich.discretescrollview.transform.Pivot;
import com.yarolegovich.discretescrollview.transform.ScaleTransformer;
import com.zhou.jiancai.R;
import com.zhou.jiancai.Utils.LogUtils;
import com.zhou.jiancai.adapter.MyAdapterImplementation;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * Created by Administrator on 2016/12/28.
 */

public class HomeFragment extends Fragment  {
    private static final int AUTO_SCROLL = 0;
  /*  @InjectView(R.id.toolbar_tv)
    TextView toolbarTv;


    @InjectView(R.id.viewpager)
    ViewPager viewpager;

    boolean baseData = true;//本地数据
    private static String[] titles;
    @InjectView(R.id.tab)
    TabLayout tab;
    @InjectView(R.id.xrefeshview)
    XRefreshView xrefeshview;
    @InjectView(R.id.toolbar)
    android.support.v7.widget.Toolbar toolbar;*/


/*    private LayoutInflater mInflater;
    private List<View> mViewList = new ArrayList<>();
    private ArrayList<String> mTitleList = new ArrayList<>();
    private RecyclerView recycleView;
    private View view11;
    private MyPagerAdapter mAdapter;
    public static int HeaderRecycleView = 0;
    public static int BodyRecycleView = 1;
    public static ArrayList<ImageView> imagelists = new ArrayList<>();
    private int[] mImageIds = new int[]{R.mipmap.test01, R.mipmap.test02,
            R.mipmap.test03};// 测试图片id
    public DiscreteScrollView scrollView;
    private int currentposition = 0;
    public DiscreteScrollViewHandler handler;
    private Runnable runnable;*/



    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);


       /* setHasOptionsMenu(true);//加上这句否则只有mainactivitytoolbar被调用
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ButterKnife.inject(this, view);
        initView();
        initData();


        intiviewpager();
        // initRecycleView();
        //关联viewpager
        // tab.setupWithViewPager(viewpager);
        // tab.setTabsFromPagerAdapter(myAdapter);
        // pulldown();
        loadMore();*/

        return view;

    }
/*
* 这是系统菜单的创建*//*
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       // super.onCreateOptionsMenu(menu, inflater);
        menu.clear();//之前在mainactivitiy调用过这里做清除
      // inflater.inflate(R.menu.menu_home,menu);
        LogUtils.d("走到这了"+"oncreateoption");

            getActivity().getMenuInflater().inflate(R.menu.menu_home, menu);

       // super.onCreateOptionsMenu(menu, inflater);

    }



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);//加上这句否则只有mainactivitytoolbar被调用
        LogUtils.d("走到这了"+"oncreate");

    }

    *//*菜单的条目的选项菜单
            * *//*@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    private void loadMore() {
        //xrefeshview.enableReleaseToLoadMore(true);


    }

    private void initRecycleView() {

        recycleView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));//获取当前的recyclede
        MyAdapter myAdapter = new MyAdapter();


        recycleView.setAdapter(myAdapter);


    }

    //自定义hanlder在oncreateview以后执行
    public class DiscreteScrollViewHandler extends Handler {
        DiscreteScrollView scrollView;

        public DiscreteScrollViewHandler(DiscreteScrollView scrollView) {
            this.scrollView = scrollView;

        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int a = (int) msg.what;


            if (a == 0) {


                //   scrollView.setItemTransitionTimeMillis(3000);


                currentposition++;
                //  Toast.makeText(getActivity(),"currentpotion"+currentposition,Toast.LENGTH_SHORT).show();


                handler.sendEmptyMessage(0);


            }
        }

    }

    class MyAdapter extends BaseRecyclerAdapter<MyAdapter.MyViewHolder> {


        @Override
        public int getAdapterItemViewType(int position) {
            if (position == 0) {
                return HeaderRecycleView;

            } else {
                return BodyRecycleView;
            }

        }


        @Override
        public MyViewHolder getViewHolder(View view) {
            View view12 = view.inflate(getActivity(), R.layout.recycleitemheader, null);
            MyViewHolder holder = new MyViewHolder(view12);//创建holder

            return holder;

        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {


            View view12 = parent.inflate(getActivity(), R.layout.recycleitem, null);
            parent.getParent().requestDisallowInterceptTouchEvent(true);
            MyViewHolder holder = new MyViewHolder(view12);//创建holder

            return holder;


        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position, boolean isItem) {


            holder.tvitem.setText("我是itm " + position);

            if (position==1){

            }
            holder.tvitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "你点击了第" + position, Toast.LENGTH_SHORT).show();
                    LogUtils.i(position + "");
                }
            });


        }


        @Override
        public int getAdapterItemCount() {
            return 50;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {


            private final TextView tvitem;

            public MyViewHolder(View itemView) {
                super(itemView);

                tvitem = (TextView) itemView.findViewById(R.id.tv_item);


            }
        }
    }


    private void intiviewpager() {
        // mAdapter = new MyPagerAdapter(mViewList);
        // viewpager.setAdapter(mAdapter);//给ViewPager设置适配器
       // tab.setupWithViewPager(viewpager);//将TabLayout和ViewPager关联起来。
       // tab.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
    }

    private void initView() {
        mInflater = LayoutInflater.from(getActivity());

        for (int i = 0; i < 7; i++) {//这里放集合
            view11 = mInflater.inflate(R.layout.textview_tab, null);
            //这里用的自定义的discreteScrollView完成自动轮播


            initDiscreteScrollView();

            recycleView = (RecyclerView) view11.findViewById(R.id.recycle_home);
            ScrollView scrollView = (ScrollView) view11.findViewById(R.id.sl);
            LinearLayout linearLayout = (LinearLayout) view11.findViewById(R.id.ll);
            scrollView.getParent().requestDisallowInterceptTouchEvent(true);
            linearLayout.getParent().requestDisallowInterceptTouchEvent(true);
            linearLayout.getChildAt(1).requestFocusFromTouch();


            recycleView.getParent().requestDisallowInterceptTouchEvent(true);

            initRecycleView();


            //添加页卡视图
            mViewList.add(view11);

        }
        scrollView.setItemTransitionTimeMillis(3000);
        scrollView.getParent().requestDisallowInterceptTouchEvent(true);


        mAdapter = new MyPagerAdapter(mViewList);
       // viewpager.setAdapter(mAdapter);//给ViewPager设置适配器


    }

    private void initDiscreteScrollView() {

        scrollView = (DiscreteScrollView) view11.findViewById(R.id.picker);
        handler = new DiscreteScrollViewHandler(scrollView);

        // MyAdapterImplementation adapterImplementation=new MyAdapterImplementation(getActivity(),mImageIds);
        //scrollView.getCurrentItem(); //returns adapter position of the currently selected item or -1 if adapter is empty.
        //position becomes selected
        //scrollView.smoothScrollToPosition(int position); //position becomes selected with animated scroll
        MyAdapterImplementation myAdapterImplementation = new MyAdapterImplementation(getActivity(), mImageIds);
        scrollView.setAdapter(myAdapterImplementation);
        for (int i = 0; i < mImageIds.length; i++) {
            // scrollView.smoothScrollToPosition(i);
            //  myAdapterImplementation.notifyDataSetChanged();

        }


        scrollView.setItemTransformer(new ScaleTransformer.Builder()
                .setMaxScale(1.05f)
                .setMinScale(0.8f)
                .setPivotX(Pivot.X.CENTER) // CENTER is a default one
                .setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one
                .build());


    }


    class MyAtuoViewPager extends PagerAdapter {


        private List<View> mAutoViewList;
        LayoutInflater mlayoutInflater;
        Context con;

        public MyAtuoViewPager(List<View> mAutoViewList, Context con) {
            this.mAutoViewList = mAutoViewList;
            this.con = con;
            mlayoutInflater = LayoutInflater.from(con);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            return mAutoViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            View view = mlayoutInflater.inflate(R.layout.home_ads_view, container, false);
            ImageView iv = (ImageView) view.findViewById(R.id.ads_view);

            iv.setBackgroundColor(Color.BLUE);
            mAutoViewList.add(iv);
            container.addView(mAutoViewList.get(position));
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView(mAutoViewList.get(position));
        }
    }

    private void initData() {
        //添加页卡标题

        //mTitleList = new ArrayList();
       // titles = new String[]{"标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7"};
      *//*  for (int i = 0; i < titles.length; i++) {
            mTitleList.add(titles[i]);
        }


        tab.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式
        for (int i = 0; i < mTitleList.size(); i++) {
            tab.addTab(tab.newTab().setText(mTitleList.get(i)));//添加tab选项卡
        }*//*
    }


    //ViewPager适配器
    class MyPagerAdapter extends PagerAdapter {
        private List<View> mViewList;

        public MyPagerAdapter(List<View> mViewList) {
            this.mViewList = mViewList;
        }

        @Override
        public int getCount() {
            return mViewList.size();//页卡数
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;//官方推荐写法
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));//添加页卡
            return mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));//删除页卡
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);//页卡标题
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mViewList.clear();
        mTitleList.clear();
        // adapter.notifyDataSetChanged();
        mAdapter.notifyDataSetChanged();
        handler.removeMessages(0);
        handler.removeCallbacks(runnable);
    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //自定义handler  否则会zai  oncreateview之前执行  导致无法初始化view
        runnable = new Runnable() {
            @Override
            public void run() {
                Message msg = Message.obtain();
                msg.what = AUTO_SCROLL;


                handler.sendMessage(msg);
            }
        };
        handler.postDelayed(runnable, 3000);

    }*/

}
